
package com.boco.webmaster.ajax.action;
import java.io.*;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.servlet.http.HttpServletRequest;

/**
 * ------------------------------------------------------------*
 *          COPYRIGHT (C) 2006 BOCO Inter-Telecom INC          *
 *   CONFIDENTIAL AND PROPRIETARY. ALL RIGHTS RESERVED.        *
 *                                                             *
 *  This work contains confidential business information       *
 *  and intellectual property of BOCO  Inc, Beijing, CN.       *
 *  All rights reserved.                                       *
 * ------------------------------------------------------------*
 *
 * PropertyMessage
 *-------------------------------------------------------------/
 /**
  *Revision Information:
  *
  *@version 1.0 2009-5-27 release(caoliang)
  */
public class PropertyMessage {

   static Properties javaProperties = null;
   static Properties javaPropertiesEn = null;
   static Properties propertyProperties = null;

    private static Properties initJavaProperties() throws IOException {
        Properties props = new Properties();
        try {
            InputStream in = new BufferedInputStream(new FileInputStream(Resources.getResourceAsFile("ApplicationResources_zh_CN.properties")));
            props.load(in);
            return props;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
    private static Properties initJavaPropertiesEn() {
            Properties props = new Properties();
            try {
                InputStream in = new BufferedInputStream(new FileInputStream(Resources.getResourceAsFile("ApplicationResources_en.properties")));
                props.load(in);
                return props;
            } catch (Exception e) {
                e.printStackTrace();
                return null;
            }
    }

    public static String getMessage(String key){
        if(javaProperties == null) {
            try {
                javaProperties = initJavaProperties();
            } catch (IOException ex) {
            }
        }
        return javaProperties.getProperty(key);
    }
    public static String getMessageEn(String key) {
            if(javaPropertiesEn == null) {
                javaPropertiesEn = initJavaPropertiesEn();
            }
            return javaPropertiesEn.getProperty(key);
    }
    public static String getRightMessage(HttpServletRequest request,String key){
        if(request==null||request.getLocale()==null||request.getLocale().getLanguage()==null||request.getLocale().getLanguage().trim().length()==0||request.getLocale().getLanguage().equals("zh")){
            return getMessage(key);
        }else{
            return getMessageEn(key);
        }
    }
    /*public static String getMessage(Object clazz,int index) {
        if(propertyProperties == null) {
            propertyProperties = initPropertyProperties();
        }
        return propertyProperties.getProperty(key);
    }*/


    private static Properties initPropertyProperties() {
        Properties props = new Properties();
        try {
            InputStream in = new BufferedInputStream(new FileInputStream("ApplicationPropertyResources_en.properties"));
            props.load(in);
            return props;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }


    public static String getPropertyMessage(String key) {
        if(propertyProperties == null) {
            propertyProperties = initPropertyProperties();
        }
        return propertyProperties.getProperty(key);
    }


    public static List getSeparatorPos(String string,String separator) {
        List list = new ArrayList();

        int postion = getPostion(string,separator,0);
        while(postion >0) {
            list.add(postion);
            postion = getPostion(string,separator,postion+1);
        }
        return list;
    }


    private static int getPostion(String string, String separator, int postion) {
        return string.indexOf(separator,postion);
    }


    public static String getPropertyMessage(String viewName, String regStr) {
//        return viewName;
        Pattern p = Pattern.compile(regStr);
        StringBuffer content = new StringBuffer(viewName);
        Matcher m = p.matcher(content);
        int i = 0;
        while (m.find()) {
//            String chinese = m.group(i);
            String chinese = viewName.substring(m.start(i)+1, m.end(i)-1);
//            String chinese = m.group();


            /*if (beforeChar.equals("\"") && afterChar.equals("\"")) {
              content.replace(m.start(1) - 1, m.end(1), "PropertyMessage.getMessage(\"" + generateResourceKey(canonicalPath, chinese, sequence++) + "\")");
            } else if (beforeChar.equals("\"") && !afterChar.equals("\"")) {
              content.replace(m.start(1) - 1, m.end(1), "PropertyMessage.getMessage(\"" + generateResourceKey(canonicalPath, chinese, sequence++) + "\") + \"");
            } else if (!beforeChar.equals("\"") && afterChar.equals("\"")) {
              content.replace(m.start(1), m.end(1) + 1, "\" + PropertyMessage.getMessage(\"" + generateResourceKey(canonicalPath, chinese, sequence++) + "\")");
            } else if (!beforeChar.equals("\"") && !afterChar.equals("\"")) {
              content.replace(m.start(1), m.end(1) + 1, "\" + PropertyMessage.getMessage(\"" + generateResourceKey(canonicalPath, chinese, sequence++) + "\") + \"");
            }*/

            String str = getPropertyMessage(chinese);
            if (str != null) {
                content.replace(m.start(i), m.end(i) , str);
            } else {
                content.replace(m.start(i), m.end(i) , "");
            }
//            i++;
            m.reset();
        }
        return content.toString();
    }
}
